;(function($){
$.Popup=$.Widget.extend({
	init: function(items,callback){
		if($.isFunction(items)&&$.isArray(callback)){
			var tmp=items; items=callback; callback=tmp;
		}
		var html='';
		items=$.isArray(items)?items:[];
		this.callback=$.isFunction(callback)?callback:$.noop;
		this.data={};
		for(var i=0;i<items.length;i++){
			var text=items[i].text;
			if(typeof(text)=='string'){
				var id=$.id(this.el);
				this.data[id]=items[i];
				html=html.concat('<tr id="',id,'"><td class="popup_content" valign="middle">',text,'</td></tr>');
			}
		}
		html=''.concat('<div class="TC_Popup" style="display:none;"><input name="__"  style="width:0.1mm;height:0.1mm;border:0px;paddind:0px;margin:0px;overflow:hidden;"><table align="left" cellpadding="0" cellspacing="0">',html,'</table></div>');
		this.el=$(html);
		this.el.appendTo(document.body);
		this.bind();
	},getEl:function(){
		return this.el;
	},bindTo:function(object){
		if(('init' in object)&&('setContextMenu' in object)){
			object.setContextMenu(this);
		}else{
			object.contextMenu=this;
		}
	},bind:function(){
		this.el.bind('mousedown',this,function(event){
			var el=$(event.srcElement||event.target);
			var pupop=event.data;
			var tmp = el.is('tr *')?el.parents('tr:first'):el.is('tr')?el:null;
			if(tmp&&tmp.size()==1){
				var data=pupop.data[$.id(tmp)];
				pupop.hide();
				pupop.callback.call(pupop,data);
			}
		});
		this.el.children('input:first').bind('blur',this,function(event){
			event.data.hide(100);
		});
		this.el.bind('mouseover',function(event){
			var el=$(event.srcElement||event.target);
			if(el.is('tr *')){el.parents('tr:first').addClass('mouseover');}
			else if(el.is('tr')){el.addClass('mouseover');}
		});
		this.el.bind('mouseout',function(event){
			var el=$(event.srcElement||event.target);
			if(el.is('tr *')){el.parents('tr:first').removeClass('mouseover');}
			else if(el.is('tr')){el.removeClass('mouseover');}
		});
	},show:function(event){
		var show=(event.button==2)||($.browser.opera&&event.ctrlKey);
		if(show||$.browser.msie){
			var x=event.clientX, y=event.clientY, width=this.el.width(), height=this.el.height();
			var css={width:width+'px',height:height+'px',top:y+'px',left:x+'px',display:'block'};
			this.el.css(css);
			this.el.children('input:first').focus();
			if(event.stopPropagation){event.stopPropagation();}
			if(window.event){window.event.cancelBubble=true;}
		}
		return this;
	},hide:function(){
		this.el.hide();return this;
	},destroy:function(){
		this.el.hide();
		this.el.children('input:first').unbind('blur');
		this.el.unbind('mouseover');
		this.el.unbind('mouseout');
	}
});
if(!$.fn.popup){
	$.fn.extend({
		popup:function(items,callback){
			return this.each(function(){
				var $this=$(this), data={el:$this,callback:$.isFunction(callback)?callback:$.noop};
				$this.bind($.browser.opera&&document.body.oncontextmenu?'mousedown':'contextmenu',data,function(event){
					var eventData=event.data;
					var popup=eventData.el.data('popup');
					popup=popup?popup:new $.Popup(items,eventData.callback);
					eventData.el.data('popup',popup);
					popup.show(event);
					return !popup.getEl().is(':visible');
				});
			});
			return this;
		}
	});
}
})(jQuery);